﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Game.Facade;
using Game.Kernel;
using Game.Utils;
using Game.Entity.Accounts;

namespace Game.Card
{
    public partial class Login : System.Web.UI.Page
    {
        /// <summary>
        /// 登录
        /// </summary>
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //获取登录数据
            string accounts = CtrlHelper.GetTextAndFilter(txtAccounts);
            string password = CtrlHelper.GetTextAndFilter(txtPassword);
            string code = CtrlHelper.GetText(txtCode);

            //数据验证
            if(string.IsNullOrEmpty(accounts))
            {
                ShowInfo("抱歉，登录账号不能为空！");
                return;
            }
            if(string.IsNullOrEmpty(password))
            {
                ShowInfo("抱歉，登录密码不能为空！");
                return;
            }
            if(string.IsNullOrEmpty(code))
            {
                ShowInfo("抱歉，验证码不能为空！");
                return;
            }
            if(!code.Equals(Fetch.GetVerifyCode(), StringComparison.InvariantCultureIgnoreCase))
            {
                ShowInfo("抱歉，验证码不正确！");
                return;
            }

            //执行登录
            Message umsg = FacadeManage.aideAccountsFacade.Logon(accounts, password);
            if(umsg.Success)
            {
                UserInfo ui = umsg.EntityList[0] as UserInfo;
                if(ui.AgentID > 0)
                {
                    AccountsAgent agent = FacadeManage.aideAccountsFacade.GetAccountAgentByUserID(ui.UserID);
                    if(agent==null || agent.Nullity == 1)
                    {
                        ShowInfo("抱歉，您的代理账号已被冻结！");
                        return;
                    }

                    Fetch.SetUserCookie(ui.ToUserTicketInfo());
                    if(GameRequest.GetQueryString("url") != "")
                        Response.Redirect(GameRequest.GetQueryString("url"));
                    else
                        Response.Redirect("/Index.aspx");
                }
                else
                {
                    ShowInfo("抱歉，您的账号不是代理账号！");
                    return;
                }
            }
            else
            {
                ShowInfo(umsg.Content);
                return;
            }
        }

        /// <summary>
        /// 弹框提示
        /// </summary>
        protected void ShowInfo(string message)
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script type='text/javascript'>layer.open({content: '" + message + "',skin: 'footer'});</script>");
        }
    }
}